from kisp import *
import random
import sys
import os

class Memory(KISP):
	Type = "Sensor"
	Name = "Memory"
	Description = "Free RAM (kB)"
	Arguments = ["Minimum Free Non Buffer/Cache RAM in kB"]
	Units = "kB"

	def Start(self,arguments):
		try:
			p = os.popen("cat /proc/meminfo")
			free = p.read().split("\n")
			free =  int(free[1].split(' ')[-2]) + int(free[2].split(' ')[-2]) + int(free[3].split(' ')[-2])
			if free > int(arguments[0]):
				return [0, free,""]
			else:
				return [1, free,""]
		except:
			return [-1,0,"Plugin Failed: %s - %s" % (sys.exc_info()[0],sys.exc_info()[1])]
